import * as sqlite_opt from "@/utils/sqliteUtil.js";

// const loginAccountTableName = "t_configs";
const configTableName = "t_configs";

export const requireSelectedPlantData = () => {
	return new Promise((resolve, reject) => {
		let sqlStr = `select 
		 cfg_value,
		 from ${configTableName} WHERE cfg_key = SelectedPlant`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireSelectedMacTypeData = () => {
	return new Promise((resolve, reject) => {
		let sqlStr = `select 
		 cfg_value,
		 from ${configTableName} WHERE cfg_key = SelectedMacType`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateSelectedPlantData = (inValue) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `update 
			${configTableName} set cfg_value = ${inValue} where cfg_key = SelectedPlant`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateSelectedMacTypeData = (inValue) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `update 
			${configTableName} set cfg_value = ${inValue} where cfg_key = SelectedMacType`;
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const requireConfigByCls = (inCls) => {
	return new Promise((resolve, reject) => {
		let sqlStr = `select * from ${configTableName} where cls = '${inCls}' `;
		console.log()
		sqlite_opt.selectSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}

export const updateConfigByKeys = (inObj) => {
	let keys = Object.keys(inObj)
	let keyStr = keys.toString()
	let valStr = ''
	let sqlStr = [];
	keys.forEach((item , index) => {
		sqlStr.push(`update '${configTableName}' set cfg_value =  '${inObj[item]}' where cfg_key = '${item}'`)
	})
	
	console.log("updateConfigByKeys")
	console.log(sqlStr)
	
	return new Promise((resolve, reject) => {
		sqlite_opt.executeSql(sqlStr,
			(e) => {
				resolve(e);
			},
			(e) => {
				console.log(e)
				reject(e);
			});
	})
}